#include "startwindow.h"
#include <QMessageBox>
#include <QRandomGenerator>

StartWindow::StartWindow(QWidget *parent)
    : QDialog(parent)
{
    setupUI();
    
    connect(m_enterBtn, &QPushButton::clicked, this, &StartWindow::onEnterClicked);
    connect(m_quitBtn, &QPushButton::clicked, this, &StartWindow::onQuitClicked);
    connect(m_usernameInput, &QLineEdit::returnPressed, this, &StartWindow::onEnterClicked);
}

StartWindow::~StartWindow()
{
}

QString StartWindow::username() const
{
    return m_usernameInput->text().trimmed();
}


void StartWindow::setupUI()
{
    setWindowTitle("本地聊天程序");
    setFixedSize(450, 250);
    
    // 设置窗口样式
    setStyleSheet("QDialog { background-color: #f0f0f0; }"
                  "QLabel { color: #333; }"
                  "QPushButton {"
                  "   background-color: #4CAF50;"
                  "   border: none;"
                  "   color: white;"
                  "   padding: 8px 16px;"
                  "   text-align: center;"
                  "   text-decoration: none;"
                  "   font-size: 14px;"
                  "   border-radius: 4px;"
                  "}"
                  "QPushButton:hover {"
                  "   background-color: #45a049;"
                  "}"
                  "QPushButton#quitBtn {"
                  "   background-color: #f44336;"
                  "}"
                  "QPushButton#quitBtn:hover {"
                  "   background-color: #da190b;"
                  "}"
                  "QLineEdit {"
                  "   padding: 8px;"
                  "   border: 1px solid #ddd;"
                  "   border-radius: 4px;"
                  "   background-color: white;"
                  "}");
    
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setSpacing(15);
    mainLayout->setContentsMargins(30, 30, 30, 30);
    
    // 标题
    QLabel *titleLabel = new QLabel("欢迎使用本地聊天程序");
    QFont titleFont = titleLabel->font();
    titleFont.setPointSize(18);
    titleFont.setBold(true);
    titleLabel->setFont(titleFont);
    titleLabel->setAlignment(Qt::AlignCenter);
    titleLabel->setStyleSheet("color: #2c3e50; margin-bottom: 10px;");
    
    // 描述文本
    QLabel *descLabel = new QLabel("请输入您的用户名开始聊天");
    descLabel->setAlignment(Qt::AlignCenter);
    descLabel->setStyleSheet("color: #7f8c8d; font-size: 12px;");
    
    // 用户名输入相关
    QLabel *nameLabel = new QLabel("用户名:");
    nameLabel->setStyleSheet("font-weight: bold; color: #2c3e50;");
    
    m_usernameInput = new QLineEdit();
    
    // 生成随机用户名作为默认值
    int randomId = QRandomGenerator::global()->bounded(1000);
    m_usernameInput->setText(QString("用户%1").arg(randomId));
    m_usernameInput->setPlaceholderText("请输入用户名...");
    
    // 按钮区域
    QHBoxLayout *btnLayout = new QHBoxLayout();
    m_enterBtn = new QPushButton("进入聊天");
    m_enterBtn->setFixedSize(120, 35);
    m_enterBtn->setDefault(true); // 设置为默认按钮，按回车可触发
    
    m_quitBtn = new QPushButton("退出");
    m_quitBtn->setObjectName("quitBtn");
    m_quitBtn->setFixedSize(120, 35);
    
    btnLayout->addStretch();
    btnLayout->addWidget(m_enterBtn);
    btnLayout->addWidget(m_quitBtn);
    btnLayout->addStretch();
    
    // 组装布局
    mainLayout->addWidget(titleLabel);
    mainLayout->addWidget(descLabel);
    mainLayout->addSpacing(20);
    mainLayout->addWidget(nameLabel);
    mainLayout->addWidget(m_usernameInput);
    mainLayout->addSpacing(10);
    mainLayout->addLayout(btnLayout);
}

void StartWindow::onEnterClicked()
{
    QString username = m_usernameInput->text().trimmed();
    if (username.isEmpty()) {
        QMessageBox::warning(this, "输入错误", "请输入用户名");
        m_usernameInput->setFocus();
        return;
    }
    
    accept(); // 关闭对话框并返回Accepted
}

void StartWindow::onQuitClicked()
{
    reject(); // 关闭对话框并返回Rejected
}
